<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/7
 * Time: 11:30
 */

namespace app\mobile\controller;


class Loupan extends Base
{
    public function _initialize(){
        parent::_initialize();
        $this->assign('action',strtolower(request()->action()));
        $id = input('param.id/d',0);
        if(!$id){
            $id = input('param.house_id/d',0);
        }
        $ads = action('home/Poster/getOne',['id'=>6],'event');
        $this->assign('id',$id);
        $this->assign('topads',$ads);
    }
    //楼盘列表页
    public function index(){
        $this->search();
        $this->getAttr();
        $this->getLists();
        $this->assign('price',$this->getPrice());
        $this->assign('title','新房');
        $this->assign('ajaxdata',true);
        return $this->fetch();
    }
    //楼盘详细页
    public function detail(){
        $id   = input('param.id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        model('house')->where(['id'=>$id])->setInc('hits');

        $this->getHousePic($id);
        $this->setSeo($info);
        $this->getRoomType($id);
        $this->getPicture($id);
        $this->assign('info',$info);
        $this->assign('title',$info['title']);

        return $this->fetch();
    }
    //详细资料
    public function info(){
        $id   = input('param.house_id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $info['seo_title'] .='详细资料';
        $this->setSeo($info);
        $this->assign('info',$info);

        return $this->fetch();
    }
    //在售户型
    public function type(){
        $id   = input('param.house_id/d',0);
        $type_id = input('param.type_id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $info['seo_title'] .='在售户型';
        $this->setSeo($info);
        $this->getRoomType($id,$type_id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //户型详细
    public function typeShow(){
        $id        = input('param.house_id/d',0);
        $type_id   = input('param.id/d',0);
        $type_info = '';
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        if($type_id){
            $type_info = model('house_type')->where(['id'=>$type_id])->find();
        }
        $info['seo_title'] .=$type_info['title'];
        $this->setSeo($info);
        $this->assign('type_info',$type_info);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //相册
    public function pic(){
        $id   = input('param.house_id/d',0);
        $cate_id = input('param.cate/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $pic_cate = cache('linkmenu_10');
        $cate     = $pic_cate['cate'];
        $count    = '';
        if($cate){
            $field = '';
            foreach($cate as $v){
                $field  .= "count(case when cate_id={$v['id']} then cate_id end) as cate_{$v['id']},";
            }
            $field .= "count(id) as total";
            $count = model('house_pic')->field($field)->where(['house_id'=>$id,'status'=>1])->find();
        }
        $where = 'status=1 and house_id='.$id;
        $cate_id && $where .= ' and cate_id='.$cate_id;
        $info['seo_title'] .='相册';
        $this->setSeo($info);
        $this->assign('where',$where);
        $this->assign('cate_id',$cate_id);
        $this->assign('picCate',$cate);
        $this->assign('count',$count);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //新闻
    public function news(){
        $id   = input('param.house_id/d',0);
        $cate_id = input('param.cate/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $where = 'status=1 and house_id = '.$info['id'];
        $cate_id && $where .= " and cate_id=".$cate_id;

        $info['seo_title'] .='资讯';
        $this->setSeo($info);
        $this->assign('info',$info);
        $this->assign('cate_id',$cate_id);
        $this->assign('where',$where);
        return $this->fetch();
    }
   //问答
    public function question(){
        $id   = input('param.house_id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $info['seo_title'] = $info['seo_title'].'问答';
        $cate = cache('askCate');
        $this->setSeo($info);
        $this->assign('info',$info);
        $this->assign('ask_cate',$cate['tree']);
        return $this->fetch();
    }
    public function getAjaxLists(){
        $page  = input('get.page/d',2);
        $return['status'] = 0;
        $where = $this->search();
        $sort  = $this->order();
        $field = 'id,title,img,house_status_name,money,tags_name,pic_count,city_name,area_name,zhuangxiu_name,is_group,sale_phone,youhui,group_youhui,average_price,price,address';
        $lists = model('house')->where($where)->field($field)->order($sort)->limit(10)->page($page)->select();
        if($lists){
            $lists = objToArray($lists);
            $return['status'] = 1;
            foreach($lists as &$v){
                $v['url'] = url('Loupan/detail',['id'=>$v['id']]);
            }
            $return['data'] = $lists;
        }
        return json($return);
    }
    private function getPicture($id){
        $cate = cache('linkmenu_10');
        $lists = model('house_pic')->where(['house_id'=>$id,'status'=>1])->limit(5)->select();
        $this->assign('picCate',$cate['cate']);
        $this->assign('picList',$lists);
    }
    //户型
    private function getRoomType($id,$type_id=0){
        $field = 'id,house_id,title,room,ting,chu,wei,yang,img,jianzhu_area';
        $lists = model('house_type')->where(['house_id'=>$id])->field($field)->select();
        $room = [];
        $data = [];
        $all  = [];
        if($lists){
            foreach($lists as $v){
                $total = isset($room[$v['room']])?$room[$v['room']]['total']:0;
                $room[$v['room']] = ['name'=>$v['room'],'total'=>$total+1];
                $data[$v['room']][] = $v;
                $all[]                = $v;
            }
        }
        $all_count = count($all);
        ksort($room);ksort($data);
        if($type_id && array_key_exists($type_id,$data)){
            $all = $data[$type_id];
        }
        $this->assign('type_id',$type_id);
        $this->assign('type_count',$all_count);
        $this->assign('room_title',$room);
        $this->assign('room_list',$data);
        $this->assign('room_all',$all);
    }


    //楼盘详细
    private function getHouseInfo($id){
        $info = model('house')->where(['id'=>$id,'status'=>1])->find();
        $info_data = model('house_data')->where(['id'=>$id])->find();
        if(!$info){
            return false;
        }
        return array_merge($info->toArray(),$info_data->toArray());
    }
    //楼盘相册 切换图
    private function getHousePic($id){
        $lists = model('house_pic')->where(['house_id'=>$id,'status'=>1])->field('img,cate_name')->group('cate_id')->select();
        $this->assign('slidePic',$lists);
    }
    private function getLists(){
        $pagesize = 10;
        $where = $this->search();
        $sort  = $this->order();
        $field = 'id,title,img,house_status_name,money,tags_name,pic_count,city_name,area_name,zhuangxiu_name,is_group,sale_phone,youhui,group_youhui,average_price,price,address';
        $lists = model('house')->where($where)->field($field)->order($sort)->limit($pagesize)->select();
        $total = model('house')->where($where)->count();
        $totalpage = ceil($total/$pagesize);
        $this->assign('totalpage',$totalpage);
        $this->assign('lists',$lists);
    }
    private function search(){
        $param['area'] = input('param.area',0);
        $param['price']      = input('param.price',0);
        $param['tags']       = input('param.tags',0);
        $param['room']       = input('param.room',0);//户型
        $param['type']       = input('param.type',0);//楼盘类型
        $param['status']     = input('param.status',0);//楼盘状态
        $param['renovation'] = input('param.renovation',0);//装修情况
        $param['floor']      = input('param.floor',0);//楼层
        $param['group']      = input('param.group/d',0);//是否团购
        $param['sort']       = input('param.sort/d',0);//排序
        $data['status']      = 1;
        $data['city']        = $this->cityInfo['id'];
        $keyword = input('get.keyword');
        if($keyword){
            $param['keyword'] = $keyword;
            $data['title'] = ['like','%'.$keyword.'%'];
        }

        if(!empty($param['area'])){
            $data['area'] = $param['area'];
        }
        if(!empty($param['price'])){
            $data['price'] = $this->getPrice($param['price']);
        }
        if(!empty($param['tags'])){
            $data[] = ['exp',"find_in_set({$param['tags']},tags_id)"];
        }
        if(!empty($param['room'])){
            $data[] = ['exp',"find_in_set({$param['room']},room_type)"];
        }
        if(!empty($param['type'])){
            $data[] = ['exp',"find_in_set({$param['type']},house_type)"];
        }
        if(!empty($param['floor'])){
            $data[] = ['exp',"find_in_set({$param['floor']},floor_type)"];
        }
        if(!empty($param['status'])){
            $data['house_status'] = $param['status'];
        }
        if(!empty($param['renovation'])){
            $data['zhuangxiu_id'] = $param['renovation'];
        }
        if(!empty($param['group'])){
            $data['is_group'] = $param['group'];
        }
        //$search = $param;
        // $current_param = array_filter($param);
        $dis_group = $param['group'] == 0 ? 1 : 0;
        $this->assign('group',$dis_group);
        $this->assign('param',$param);
        $this->assign('search',$param);
        return $data;
    }
   private function getAttr($return=''){
        $tags     = cache('linkmenu_5');//特色标签
        $status   = cache('linkmenu_1');//楼盘状态
        $room     = cache('linkmenu_6');//户型类型
        $type     = cache('linkmenu_7');//楼盘类型
        $floor    = cache('linkmenu_8');//楼层类型
        $renovation = cache('linkmenu_9');//装修程度
        if($return){
            $temp = cache('attr_menu');
            if(!$temp){
                $data = array_merge($tags['cate'],$status['cate'],$room['cate'],$type['cate'],$floor['cate'],$renovation['cate']);
                $temp = [];
                foreach($data as $v){
                    $temp[$v['id']] = $v;
                }
                cache('attr_menu',$temp,3600);
            }
            return $temp;
        }else{
            $this->assign('tags',$tags['cate']);
            $this->assign('status',$status['cate']);
            $this->assign('room',$room['cate']);
            $this->assign('type',$type['cate']);
            $this->assign('floor',$floor['cate']);
            $this->assign('renovation',$renovation['cate']);
        }
    }

    private function getPrice($key=''){
        $data = [
            1 => ['name'=>'5000以下','value'=>[0,5000]],
            2 => ['name'=>'5000-8000','value'=>[5000,8000]],
            3 => ['name'=>'8000-10000','value'=>[8000,10000]],
            4 => ['name'=>'10000-15000','value'=>[10000,15000]],
            5 => ['name'=>'15000以上','value'=>'15000']
        ];
        if($key){
            if($key == 5){
                $str = ['egt',$data[$key]['value']];
            }else{
                $str = ['between',$data[$key]['value']];
            }
        }else{
            $str = $data;
        }
        return $str;
    }
    //排序
    private function order(){
        $sort = input('param.sort/d',0);
        switch($sort){
            case 1:
                $order = 'price asc';
                break;
            case 2:
                $order = 'price desc';
                break;
            default:
                $order = 'ordid asc,id desc';
                break;
        }
        $dis_sort = $sort == 1 ? 2 : 1;
        $this->assign('sort',$dis_sort);
        return $order;
    }

}